/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import 'package:adaptive_layout_sample/routes/routers.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:hadss_adaptive_layout/hadss_adaptive_layout.dart';
import 'package:provider/provider.dart';

import 'l10n/generated/app_localizations.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  final routeManager = RouteManager();
  runApp(ChangeNotifierProvider(
      create: (context) => routeManager, child: const MyApp()));
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      localizationsDelegates: const [
        AppLocalizations.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
      ],
      supportedLocales: const [
        Locale('en', 'US'), // 英语
        Locale('zh', 'CN'), // 中文
      ],
      locale: const Locale('zh', 'CN'),
      theme: ThemeData(
          appBarTheme: const AppBarTheme(
        toolbarHeight: 5,
        elevation: 0,
      )),
      debugShowCheckedModeBanner: false,
      initialRoute: '/',
      onGenerateRoute: onGenerateRoute,
    );
  }
}

class RouteManager extends ChangeNotifier {
  final ValueNotifier<List<Page>> pages = ValueNotifier([]);
  WidthBreakpoint _widthBreakpoint = WidthBreakpoint.unKnow;
  HeightBreakpoint _heightBreakpoint = HeightBreakpoint.unKnow;

  /// 断点状态需要初始化一次
  RouteManager() {
    _initBreakpoint();
  }

  Future<void> _initBreakpoint() async {
    final BreakpointData data = await BreakpointManager().init();
    _widthBreakpoint = data.widthBreakpoint;
    _heightBreakpoint = data.heightBreakpoint;
    BreakpointManager().addBreakpointCallback((breakpointData) {
      _widthBreakpoint = breakpointData.widthBreakpoint;
      _heightBreakpoint = breakpointData.heightBreakpoint;
      notifyListeners();
    });
  }

  WidthBreakpoint get widthBreakpoint => _widthBreakpoint;

  HeightBreakpoint get heightBreakpoint => _heightBreakpoint;

  pushPage(Page page) {
    final existing = pages.value.any((p) => p.key == page.key);
    if (!existing) {
      pages.value = [...pages.value, page];
    }
  }

  popPage() {
    if (pages.value.length > 1) {
      pages.value = [...pages.value..removeLast()];
    }
  }

  void replaceAll(List<Page> newPages) {
    pages.value = newPages;
  }
}
